<template>
  <div class="container">
    <h1 class="title">快跑预测</h1>
    <div class="buttons">
      <button class="cos-true" @click="fetchPredictionResults('cos', true)">算法C1(isFront: true)</button>
      <button class="cos-false" @click="fetchPredictionResults('cos', false)">算法C2(isFront: false)</button>
      <button class="sin-true" @click="fetchPredictionResults('sin', true)">算法S1(isFront: true)</button>
      <button class="sin-false" @click="fetchPredictionResults('sin', false)">算法S2(isFront: false)</button>
      <button class="genhao-true" @click="fetchPredictionResults('genhao', true)">算法M1(isFront: true)</button>
      <button class="genhao-false" @click="fetchPredictionResults('genhao', false)">算法M2(isFront: false)</button>
      <button class="log-true" @click="fetchPredictionResults('log', true)">算法L1(isFront: true)</button>
      <button class="log-false" @click="fetchPredictionResults('log', false)">算法L2(isFront: false)</button>
    </div>
    <div class="countdown-section">
      <div class="countdown">下一次刷新倒计时: {{ countdownDisplay }}</div>
      <button class="refresh-button" @click="manualRefresh">手动刷新</button>
    </div>
    <div v-if="predictions.length" class="results">
      <table class="prediction-table">
        <thead>
        <tr>
          <th>时间</th>
          <th>本期开奖</th>
          <th>预测内容</th>
          <th>预测大小</th>
          <th>预测单双</th>
        </tr>
        </thead>
        <tbody>
        <tr v-for="(prediction, index) in predictions" :key="index">
          <td>{{ prediction.CurrentOpenTime }}</td>
          <td>{{ prediction.PreviousOpencode }}</td>
          <td>{{ prediction.PredictedSum }}</td>
          <td :class="{ correct: prediction.SizeCorrect, incorrect: !prediction.SizeCorrect && index !== 0 }">
            {{ prediction.PredictedSize }} <span v-if="index !== 0">{{ prediction.SizeCorrect ? '正确' : '错误' }}</span>
          </td>
          <td :class="{ correct: prediction.OddEvenCorrect, incorrect: !prediction.OddEvenCorrect && index !== 0 }">
            {{ prediction.PredictedOddEven }} <span v-if="index !== 0">{{ prediction.OddEvenCorrect ? '正确' : '错误' }}</span>
          </td>
        </tr>
        </tbody>
      </table>
    </div>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      predictions: [],
      countdownDisplay: '',
      countdownTimer: null,
    };
  },
  methods: {
    fetchPredictionResults(type, isFront) {
      axios.get(`http://127.0.0.1:8081/forecast/get${type}?isFront=${isFront}`)
          .then(response => {
            this.predictions = response.data.data;
            this.setNextRefreshTime();
          })
          .catch(error => {
            console.error('Error fetching prediction results:', error);
          });
    },
    manualRefresh() {
      if (this.predictions.length > 0) {
        const lastPrediction = this.predictions[0];
        const openTime = new Date(lastPrediction.CurrentOpenTime);
        const now = new Date();
        const diff = (openTime.getTime() + 210000 - now.getTime()) / 1000;
        if (diff <= 0) {
          this.fetchPredictionResults('cos', true);
        }
      }
    },
    setNextRefreshTime() {
      if (this.countdownTimer) {
        clearInterval(this.countdownTimer);
      }
      if (this.predictions.length > 0) {
        const lastPrediction = this.predictions[0];
        const openTime = new Date(lastPrediction.CurrentOpenTime);
        const nextRefreshTime = new Date(openTime.getTime() + 210000);
        this.startCountdown(nextRefreshTime);
      }
    },
    startCountdown(endTime) {
      const updateCountdown = () => {
        const now = new Date();
        const timeDiff = endTime - now;
        if (timeDiff <= 0) {
          clearInterval(this.countdownTimer);
          this.fetchPredictionResults('cos', true);
        } else {
          const minutes = Math.floor((timeDiff % (1000 * 60 * 60)) / (1000 * 60));
          const seconds = Math.floor((timeDiff % (1000 * 60)) / 1000);
          this.countdownDisplay = `${minutes}:${seconds < 10 ? '0' : ''}${seconds}`;
        }
      };
      this.countdownTimer = setInterval(updateCountdown, 1000);
      updateCountdown();
    },
  },
};
</script>

<style scoped>
.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
  background-color: #e0f7fa;
  border-radius: 10px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.title {
  text-align: center;
  color: #0288d1;
  margin-bottom: 20px;
  font-size: 2em;
}

.buttons {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
  flex-wrap: wrap;
  gap: 10px;
}

button {
  background-color: #0288d1;
  color: #fff;
  border: none;
  padding: 8px 15px;
  border-radius: 5px;
  cursor: pointer;
  font-size: 14px;
  margin: 5px;
  transition: background-color 0.3s;
}

button:hover {
  background-color: #0277bd;
}

.cos-true {
  background-color: #ff8a80;
}

.cos-false {
  background-color: #ff5252;
}

.sin-true {
  background-color: #ff80ab;
}

.sin-false {
  background-color: #ff4081;
}

.genhao-true {
  background-color: #ea80fc;
}

.genhao-false {
  background-color: #e040fb;
}

.log-true {
  background-color: #8c9eff;
}

.log-false {
  background-color: #536dfe;
}

.countdown-section {
  text-align: center;
  margin-bottom: 20px;
}

.countdown {
  font-size: 1.5em;
  margin-bottom: 10px;
}

.refresh-button {
  background-color: #00796b;
  color: #fff;
  padding: 10px 20px;
  border-radius: 5px;
  cursor: pointer;
  font-size: 16px;
  transition: background-color 0.3s;
}

.refresh-button:hover {
  background-color: #004d40;
}

.prediction-table {
  width: 100%;
  border-collapse: collapse;
}

.prediction-table th,
.prediction-table td {
  border: 1px solid #ddd;
  padding: 8px;
  text-align: center;
}

.prediction-table th {
  background-color: #b3e5fc;
}

.correct {
  background-color: #d4edda;
  color: #155724;
}

.incorrect {
  background-color: #f8d7da;
  color: #721c24;
}
</style>
